/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.fzhucc.modules.thinktank.service;

import com.fzhucc.exception.BadRequestException;
import com.fzhucc.modules.expert.domain.Expert;
import com.fzhucc.modules.expert.domain.dto.ExpertMsgBoxDto;
import com.fzhucc.modules.thinktank.domain.Academician;
import com.fzhucc.modules.thinktank.domain.AcademicianMsgBox;
import com.fzhucc.modules.thinktank.domain.dto.AcademicianBoxSeekDto;
import com.fzhucc.modules.thinktank.domain.dto.AcademicianMsgBoxDto;
import com.fzhucc.modules.thinktank.domain.dto.AcademicianMsgBoxQueryCriteria;
import com.fzhucc.modules.thinktank.repository.AcademicianMsgBoxRepository;
import com.fzhucc.modules.thinktank.repository.AcademicianRepository;
import com.fzhucc.modules.thinktank.service.mapstruct.AcademicianMsgBoxMapper;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.PageUtil;
import com.fzhucc.utils.QueryHelp;
import com.fzhucc.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.*;

/**
 * @author yyg
 * @website https://eladmin.vip
 * @description 服务实现
 * @date 2023-03-10
 **/
@Service
@RequiredArgsConstructor
public class AcademicianMsgBoxService {

    private final AcademicianMsgBoxRepository academicianMsgBoxRepository;

    private final AcademicianRepository academicianRepository;
    private final AcademicianMsgBoxMapper academicianMsgBoxMapper;

    public Map<String, Object> queryAll(AcademicianMsgBoxQueryCriteria criteria, Pageable pageable) {
        Page<AcademicianMsgBox> page = academicianMsgBoxRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        Page<AcademicianMsgBoxDto> map = page.map(academicianMsgBoxMapper::toDto);
        List<AcademicianMsgBoxDto> list = map.getContent();
        if (list != null) {
            for (AcademicianMsgBoxDto academicianMsgBoxDto : list) {
                Optional<Academician> res = academicianRepository.findById(academicianMsgBoxDto.getAcademicianId());
                if (res.isPresent()) {
                    academicianMsgBoxDto.setAcademician(res.get());
                }
            }
        }
        return PageUtil.toPage(map);
    }

    public List<AcademicianMsgBoxDto> queryAll(AcademicianMsgBoxQueryCriteria criteria) {
        return academicianMsgBoxMapper.toDto(academicianMsgBoxRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder)));
    }

    public AcademicianMsgBoxDto findById(Integer id) {
        AcademicianMsgBox academicianMsgBox = academicianMsgBoxRepository.findById(id).orElseGet(AcademicianMsgBox::new);
        ValidationUtil.isNull(academicianMsgBox.getId(), "AcademicianMsgBox", "id", id);
        return academicianMsgBoxMapper.toDto(academicianMsgBox);
    }

    @Transactional(rollbackFor = Exception.class)
    public AcademicianMsgBoxDto create(AcademicianMsgBox resources) {
        return academicianMsgBoxMapper.toDto(academicianMsgBoxRepository.save(resources));
    }

    @Transactional(rollbackFor = Exception.class)
    public void update(AcademicianMsgBox resources) {
        AcademicianMsgBox academicianMsgBox = academicianMsgBoxRepository.findById(resources.getId()).orElseGet(AcademicianMsgBox::new);
        ValidationUtil.isNull(academicianMsgBox.getId(), "AcademicianMsgBox", "id", resources.getId());
        academicianMsgBox.copy(resources);
        academicianMsgBoxRepository.save(academicianMsgBox);
    }

    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            academicianMsgBoxRepository.deleteById(id);
        }
    }

    public void download(List<AcademicianMsgBoxDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AcademicianMsgBoxDto academicianMsgBox : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put("院士之家id", academicianMsgBox.getAcademicianId());
            map.put("咨询内容", academicianMsgBox.getContent());
            map.put("联系方式", academicianMsgBox.getPhone());
            map.put("咨询时间", academicianMsgBox.getConsultationTime());
            map.put("咨询人姓名", academicianMsgBox.getName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Transactional(rollbackFor = Exception.class)
    public AcademicianMsgBoxDto seek(AcademicianBoxSeekDto resources) {
        AcademicianMsgBox academicianMsgBox = new AcademicianMsgBox();
        Optional<Academician> res = academicianRepository.findById(resources.getAcademicianId());
        if (!res.isPresent()) {
            throw new BadRequestException("未找到该院士之家");
        }
        academicianMsgBox.setAcademicianId(resources.getAcademicianId());
        academicianMsgBox.setPhone(resources.getPhone());
        academicianMsgBox.setConsultationTime(new Timestamp(new Date().getTime()));
        academicianMsgBox.setContent(resources.getContent());
        academicianMsgBox.setName(resources.getName());
        AcademicianMsgBox academicianMsgBox1 = academicianMsgBoxRepository.save(academicianMsgBox);
        return academicianMsgBoxMapper.toDto(academicianMsgBox1);
    }
}